package ch.zbw.sysventory.server.control;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import ch.zbw.sysventory.server.model.User;
import ch.zbw.sysventory.server.service.security.UserService;

@Controller
@RequestMapping("/")
public class WelcomeController {

	@Autowired(required = true)
	private UserService userService;

	@RequestMapping
	public ModelAndView welcomePage(ModelMap modelMap) {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		if (!(auth instanceof AnonymousAuthenticationToken)) {
			User user = this.userService.getByLoginName(auth.getName());
			modelMap.addAttribute("user", user);
		}
		return new ModelAndView("welcome");
	}

}
